﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace PlayBuzz
{
    public partial class FormBuzz : Form
    {
        public FormBuzz()
        {
            InitializeComponent();
        }

        private void btnBuzz_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txbFile.Text))
            {
                //DialogResult result = ofdOpenMedia.ShowDialog();
                //if ((ofdOpenMedia.InitialDirectory == null) || (ofdOpenMedia.InitialDirectory == string.Empty))
                //{
                //    ofdOpenMedia.InitialDirectory = "c:"; // 語音檔預設開啟目錄
                //}

                ofdOpenMedia.Filter = "Audio files (*.wav; *.mp3; )|*.wav; *.mp3;";
                //        "Audio files (*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma)|*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma|" +
                //        "Video Files (*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv)|*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv|" +
                //        "MIDI Files (*.mid, *.midi, *.rmKKTimeri)|*.mid; *.midi; *.rmi|" +
                //        "Image Files (*.jpg, *.bmp, *.gif, *.tga)|*.jpg; *.bmp; *.gif; *.tga|" +
                //"All Files (*.*)|*.*";
                ofdOpenMedia.Title = "請開啟語音檔";
                ofdOpenMedia.Multiselect = false; // 允許選取多檔案

                // Step 3: 開啟對話框
                if (ofdOpenMedia.ShowDialog(this) == System.Windows.Forms.DialogResult.Cancel)
                {
                    // 使用者沒有選檔案
                    return;
                }

                txbFile.Text = ofdOpenMedia.FileName; // 選擇的完整路徑
                //PlayAsync(Filename);
            }

            //PlayAsync(txbFile.Text);
            SoundPlayerAppartment player = new SoundPlayerAppartment(txbFile.Text);
            player.PlayOne();
        }

        public class SoundPlayerAppartment
        {
            public string FilePath { get; set; }
            public SoundPlayerAppartment(string filepath)
            {
                FilePath = filepath;
                threadsyncobj = new AutoResetEvent(false);
            }

            private AutoResetEvent threadsyncobj;

            public void PlayOne()
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(PlayAsync));
            }

            public void Play(int numberOfTimes)
            {
                for (int i = 0; i < numberOfTimes; i++)
                {
                    PlayOne();
                    //shit, this block the GUI
                    //threadsyncobj.WaitOne();
                }
            }

            private void PlaySync()
            {
                System.Media.SoundPlayer player = new
         System.Media.SoundPlayer();
                player.SoundLocation = FilePath;
                player.Load();
                player.Play();
            }
            //异步播放：
            private void PlayAsync(Object state)
            {
                System.Media.SoundPlayer player = new
                 System.Media.SoundPlayer();
                player.SoundLocation = FilePath;
                player.LoadAsync();
                player.PlaySync();
                //threadsyncobj.Set();
            }
            //循环播放：
            private void PlayLoop()
            {
                System.Media.SoundPlayer player = new
                 System.Media.SoundPlayer();
                player.SoundLocation = FilePath;
                player.Load();
                player.PlayLooping();
            }
        }

        private void btnBuzz2_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txbFile.Text))
            {
                //DialogResult result = ofdOpenMedia.ShowDialog();
                //if ((ofdOpenMedia.InitialDirectory == null) || (ofdOpenMedia.InitialDirectory == string.Empty))
                //{
                //    ofdOpenMedia.InitialDirectory = "c:"; // 語音檔預設開啟目錄
                //}

                ofdOpenMedia.Filter = "Audio files (*.wav; *.mp3; )|*.wav; *.mp3;";
                //        "Audio files (*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma)|*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma|" +
                //        "Video Files (*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv)|*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv|" +
                //        "MIDI Files (*.mid, *.midi, *.rmKKTimeri)|*.mid; *.midi; *.rmi|" +
                //        "Image Files (*.jpg, *.bmp, *.gif, *.tga)|*.jpg; *.bmp; *.gif; *.tga|" +
                //"All Files (*.*)|*.*";
                ofdOpenMedia.Title = "請開啟語音檔";
                ofdOpenMedia.Multiselect = false; // 允許選取多檔案

                // Step 3: 開啟對話框
                if (ofdOpenMedia.ShowDialog(this) == System.Windows.Forms.DialogResult.Cancel)
                {
                    // 使用者沒有選檔案
                    return;
                }

                txbFile.Text = ofdOpenMedia.FileName; // 選擇的完整路徑
                //PlayAsync(Filename);
            }

            //PlayAsync(txbFile.Text);
            SoundPlayerAppartment player = new SoundPlayerAppartment(txbFile.Text);
            player.Play(2);
        }
    }
}
